﻿//全局变量-未选操作行提示
var nullrowmsg = "请选择操作的行!";

//全局变量-未选删除行提示
var delnullrowmsg = "请选择要删除的行!";

//初始化Tabs
$(function () {
    $("#layout-tabs").tabs({
        fit: true, border: false, tools: "#layout-tabs-tools",
        onContextMenu: function (e, title) {
            e.preventDefault();
            $("#layout-tabs-menu").menu("show", {
                left: e.pageX, top: e.pageY
            }).data("tabTitle", title);
        }
    });
    $("#layout-tabs-menu").menu({
        onClick: function (item) {
            var curTabTitle = $(this).data("tabTitle"), type = $(item.target).attr("type");

            if (type == "refresh") {
                $("#layout-tabs").tabs("getTab", curTabTitle).panel("refresh");
                return;
            }

            if (type == "close") {
                var t = $("#layout-tabs").tabs("getTab", curTabTitle);
                if (t.panel("options").closable) {
                    $("#layout-tabs").tabs("close", curTabTitle);
                }
                return;
            }

            var allTabs = $("#layout-tabs").tabs("tabs"), closeTabsTitle = [];

            $.each(allTabs, function () {
                var opt = $(this).panel("options");
                if (opt.closable && opt.title != curTabTitle && type == "closeOther") {
                    closeTabsTitle.push(opt.title);
                }
                else if (opt.closable && type == "closeAll") { closeTabsTitle.push(opt.title); }
            });

            for (var i = 0; i < closeTabsTitle.length; i++) { $("#layout-tabs").tabs("close", closeTabsTitle[i]); }
        }
    });
});

//添加Tab
function AddTab(title, url, icon) {
    var layouttabs = $("#layout-tabs");

    if (layouttabs.tabs("exists", title)) {
        layouttabs.tabs("select", title);
    }
    else {
        if (url) {
            $.AjaxMask({
                url: url, type: "get",
                success: function (result) {
                    layouttabs.tabs("add", {
                        title: title,
                        iconCls: icon,
                        content: result,
                        closable: true,
                        style: { padding: "1px" }
                    });
                }
            });
        }
    }
}

//转到主页
function GoToHomeTab() {
    $("#layout-tabs").tabs("select", "欢迎使用");
}

//关闭当前Tab
function CloseSelectedTab() {
    var opt = $("#layout-tabs").tabs("getSelected").panel("options");

    if (opt.closable) { $("#layout-tabs").tabs("close", opt.title); }
}

//关闭全部Tab
function CloseAllTabs() {
    $.EasyuiConfirm("确认关闭所有选项卡?", function (ec) {
        if (ec) {
            var allTabs = $("#layout-tabs").tabs("tabs"), closeTabsTitle = [];

            $.each(allTabs, function () {
                var opt = $(this).panel("options");

                if (opt.closable) { closeTabsTitle.push(opt.title); }
            });

            for (var i = 0; i < closeTabsTitle.length; i++) {
                $("#layout-tabs").tabs("close", closeTabsTitle[i]);
            }
        }
    });
}

//添加Dialog
function AddDialog(url) {
    if (url) {
        $.AjaxMask({
            url: url, type: "get",
            success: function (result) {
                $("#layout-common-dialog").html(result);
            }
        });
    }
}

//切换主题
function ChangeTheme(theme) {
    var themes = "../public/easyui/themes/" + theme + "/easyui.css";

    $("#main-themes").attr("href", themes);
    $.cookie("themes", theme, { expires: 7 });
}

//修改密码
function SetPwd() {
    $.ModelDialog({
        title: "修改密码", width: 260,
        href: "sys/user/EditPwd.aspx",
        handler: function () {
            $.ModelForm({
                validate: true, button: $(this),
                id: "#user-editpwd",
                url: $.Api("Sys/User/EditPwd"),
                success: function (result) { }
            });
        },
        onLoad: function () { }
    });
}

//注销登录
function Logout() {
    $.EasyuiConfirm("确认注销?", function (ec) {
        if (ec) {
            location.href = "../Login.aspx"; return false;
        }
    });
}

//菜单事件
var MenuOnClick = function (node) {
    switch (node.attributes.type) {
        case "Tab":
            AddTab(node.text, node.attributes.href, node.iconCls); break;
        case "Dialog":
            AddDialog(node.attributes.href); break;
        default: break;

    }
}

//菜单初始化
var MenuloadFilter = function (data, parent) {
    return data;
}